//
//  YFCheckNetworkStatus.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/14.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFCheckNetworkStatus.h"



NSString *const NetworkStatusChangedNotification = @"YFNetworkReadchabilityStatusChanged";

@implementation YFCheckNetworkStatus

+ (instancetype)shareInstance {
    static YFCheckNetworkStatus *_checkNetwork;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _checkNetwork = [[YFCheckNetworkStatus alloc] init];
    });
    return _checkNetwork;
}

- (instancetype)init {
    if (self = [super init]) {
        [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            YFLog(@"Reachability:%@",AFStringFromNetworkReachabilityStatus(status));
            self.networkStatus = status;
            // 发送系统通知
            [[NSNotificationCenter defaultCenter] postNotificationName:NetworkStatusChangedNotification object:@(self.networkStatus)];
        }];
        [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    }
    return self;
}

+ (BOOL)isReachable {
    return [YFCheckNetworkStatus shareInstance].networkStatus != AFNetworkReachabilityStatusNotReachable;
}
+ (BOOL)isViaWWAN {
    return [YFCheckNetworkStatus shareInstance].networkStatus == AFNetworkReachabilityStatusReachableViaWWAN;
}
+ (BOOL)isWiFi {
    return [YFCheckNetworkStatus shareInstance].networkStatus == AFNetworkReachabilityStatusReachableViaWiFi;
}


@end
